package com.scoy.honeymei.dialog;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.HttpParams;
import com.scoy.honeymei.adapter.ShopTicketCheckAdapter;
import com.scoy.honeymei.bean.ShopTicketBean;
import com.scoy.honeymei.databinding.DialogTicketCheckBinding;
import com.scoy.honeymei.http.HpGo;
import com.scoy.honeymei.url.ApiCallBack;
import com.scoy.honeymei.url.MeConstant;
import com.scoy.honeymei.url.MyUrl;
import com.scoy.honeymei.utils.LogUtil;
import com.scoy.honeymei.utils.MyUtil;
import com.scoy.honeymei.utils.SPHelper;

import java.util.List;

/**
 * ClassName:      com.oylib.dialog
 * Description:
 * Author:         GaoZhan & Administrator
 * UpdateDate:     2019/12/31/0031$ 下午 15:57$
 * UpdateRemark:   更新内容
 */
public class TicketCheckDialog extends DialogFragment {


    private DialogTicketCheckBinding binding;
    private Context mContext;
    private int shopId;
    private String price;
    private ShopTicketCheckAdapter hAdapter;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            shopId = getArguments().getInt("shopId", 0);
            if(getArguments().getString("price")!=null){
                price = getArguments().getString("price");
            }

        }
    }

    public static TicketCheckDialog newInstance(int shopId) {

        Bundle args = new Bundle();
        args.putInt("shopId", shopId);
        TicketCheckDialog fragment = new TicketCheckDialog();
        fragment.setArguments(args);
        return fragment;
    }

    public static TicketCheckDialog newInstance(int shopId,String price) {

        Bundle args = new Bundle();
        args.putInt("shopId", shopId);
        args.putString("price", price);
        TicketCheckDialog fragment = new TicketCheckDialog();
        fragment.setArguments(args);
        return fragment;
    }
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        binding = DialogTicketCheckBinding.inflate(inflater, container, false);
        mContext = getContext();

        initView();

        httpDataList();

        return binding.getRoot();
    }

    private void initView() {

        initRv();
        binding.dtcSureTv.setOnClickListener(v -> {
            if (!MyUtil.isFastClick()) return;
            List<ShopTicketBean> data = hAdapter.getData();
            if (data.size() == 0) return;
            boolean check = false;
            ShopTicketBean ccBean = new ShopTicketBean();
            for (ShopTicketBean cBean : data) {
                if (cBean.isCheck()) {
                    check = true;
                    ccBean = cBean;
                    break;
                }
            }
            if (!check) return;
            onDialogListener.onSuccess(ccBean);
            dismiss();
        });

        binding.dtcCloseIv.setOnClickListener(v -> dismiss());

    }

    @Override
    public void onStart() {
        super.onStart();
        Window dialogWindow = getDialog().getWindow();
        if (dialogWindow != null) {
            dialogWindow.getDecorView().setPadding(0, 0, 0, 0);
            dialogWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            dialogWindow.setGravity(Gravity.BOTTOM);
            WindowManager.LayoutParams lp = dialogWindow.getAttributes();
            lp.width = WindowManager.LayoutParams.MATCH_PARENT;
            lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
            lp.gravity = Gravity.BOTTOM;
            lp.windowAnimations = android.R.style.Animation_InputMethod;
            dialogWindow.setAttributes(lp);
        }
    }

    private void initRv() {
        hAdapter = new ShopTicketCheckAdapter(mContext);
        binding.dtcRv.setLayoutManager(new LinearLayoutManager(mContext, RecyclerView.VERTICAL, false));
        binding.dtcRv.setAdapter(hAdapter);
        hAdapter.setOnOneClick(pos -> {
            List<ShopTicketBean> data = hAdapter.getData();
            if(Double.parseDouble(price)<Double.parseDouble(data.get(pos).getCondition())){
                MyUtil.mytoast(getContext(),"不符合优惠券规则");
                return;
            }
            for (int i = 0; i < data.size(); i++) {
                ShopTicketBean zzBean = data.get(i);
                if (pos == i) {
                    if (zzBean.isCheck()) {
                        return;
                    } else {
                        zzBean.setCheck(true);
                        hAdapter.notifyItemChanged(i);
                    }
                } else {
                    if (zzBean.isCheck()) {
                        zzBean.setCheck(false);
                        hAdapter.notifyItemChanged(i);
                    }
                }
            }


        });

    }

    private void httpDataList() {
        HttpParams params = new HttpParams();
        params.put("shop_id", shopId);
        params.put("user_id", SPHelper.getString(MeConstant.UID, ""));
        HpGo.newInstance().HttpGo(getActivity(), MyUrl.SHOP_COUPON_ME, params, new ApiCallBack() {
            @Override
            public void onSuccess(String o) {
                List<ShopTicketBean> data = new Gson().fromJson(o, new TypeToken<List<ShopTicketBean>>() {
                }.getType());

                hAdapter.setNewData(data);
            }

        });

    }


    public interface OnDialogListener {
        void onSuccess(ShopTicketBean ccBean);

    }

    OnDialogListener onDialogListener;

    public TicketCheckDialog setOnDialogListener(OnDialogListener onDialogListener) {
        this.onDialogListener = onDialogListener;
        return this;
    }


}
